require("dplyr")

## Set working directory
## to Dataverse folder

survey <- read.table("Harris_Data/Harris 1993 Omnibus Public Opinion Survey, study no. 931108/harris_s931108_spss.tab", header = TRUE)


# pid
survey$pid <- c(1:nrow(survey))

# study 
survey$study <- "931108"

# study year (year)
survey$year <- 1993

# geographic data (urban)
survey$urban <- NA

# geographic data (region)
survey$region <- NA

# respondent head of household (hh)
survey$hh <- NA

# increasing inequality (inequality)
table(survey$B1_2)
survey$inequality <- dplyr::recode(as.character(survey$B1_2),
                            `1` = "Feel",
                            `2` = "Don't Feel",
                            `3` = "Not Sure")
table(survey$inequality)

# inequality variable (inequality.variable)
survey$inequality.variable <- 1

# union (union.self)
table(survey$L7A_1)
survey$union.self <- dplyr::recode(survey$L7A_1,
                            `0` = "No",
                            `1` = "Yes")
table(survey$union.self)

survey$union.other <- dplyr::recode(survey$L7A_2,
                             `0` = "No",
                             `1` = "Yes")
table(survey$union.other)

table(survey$L7A_4) # not sure
survey[survey$L7A_4 == 1, c("union.self", "union.other", "L7A_3")]

survey$union.self[survey$L7A_4 == 1] <- "Not Sure"
survey$union.other[survey$L7A_4 == 1] <- "Not Sure"

# employment (employed)
survey$employed <- NA

# empl self
table(survey$L8)
survey$employed.self <- dplyr::recode(survey$L8,
                               `1` = "Full-time",
                               `2` = "Part-time",
                               `3` = "Retired",
                               `4` = "Unemployed",
                               `5` = "Student",
                               `6` = "Not working",
                               `7` = "Other",
                               `8` = "Not sure")
table(survey$employed.self)

# occupation
survey$occupation <- NA

# occ self
survey$occupation.self <- NA

# household size (hhsize)
## just adults
table(survey$F8)
survey$hhsize <- as.character(survey$F8)
table(survey$hhsize)

# education (educ)
table(survey$F2)
survey$educ <- dplyr::recode(survey$F2,
                      `1` = "Less than high school",
                      `2` = "High school graduate",
                      `3` = "Some college",
                      `4` = "College graduate",
                      `5` = "Post graduate",
                      `6` = "Not sure")
table(survey$educ)

# household income (income)
table(survey$F5)
survey$income <- dplyr::recode(survey$F5,
                        `1` = "Under $7500",
                        `2` = "$7,501 to $15,000",
                        `3` = "$15,001 to $25,000",
                        `4` = "$25,001 to $35,000",
                        `5` = "$35,001 to $50,000",
                        `6` = "$50,001 to $75,000",
                        `7` = "$75,001 to $100,000",
                        `8` = "$100,001 or over",
                        `9` = "Not sure/refused")
table(survey$income)

# age
table(survey$F1)
survey$age <- as.character(survey$F1)
table(survey$F1)

# race
table(survey$F7)
survey$race1 <- dplyr::recode(survey$F7,
                      `1` = "White",
                      `2` = "Black",
                      `3` = "African-American",
                      `4` = "Asian or Pacific Islander",
                      `5` = "American Indian or Alaskan native",
                      `6` = "Some other race",
                      `7` = "Not sure")
survey$race2 <- dplyr::recode(survey$F6,
                              `3` = "Decline/not sure",
                              `1` = "Yes, hispanic",
                              `2` = "No, not hispanic")
table(survey$race1)
table(survey$race2)
sum(is.na(survey$race2))
survey$race <- ifelse(survey$race1 == "Not sure", 
                      "Decline/not sure", ifelse(survey$race1 == "White",
                                                 ifelse(survey$race2 == "Decline/not sure",
                                                        "Decline/not sure",
                                                        ifelse(survey$race2 == "No, not hispanic",
                                                               "Non-Hispanic White",
                                                               "Hispanic White")), "Non-white"))
table(survey$race)
table(survey$race[survey$race1 == "White"])

# politics (party)
table(survey$F3A)
survey$party <- dplyr::recode(survey$F3A,
                       `1` = "Republican",
                       `2` = "Democrat",
                       `3` = "Independent",
                       `4` = "Other",
                       `5` = "Not sure")
table(survey$party)

# politics (ideology)
table(survey$F3B)
survey$ideology <- dplyr::recode(survey$F3B,
                          `1` = "Conservative",
                          `2` = "Moderate",
                          `3` = "Liberal",
                          `4` = "Not sure")
table(survey$ideology)

# gender
table(survey$SEX)
survey$gender <- dplyr::recode(survey$SEX,
                        `1` = "Male",
                        `2` = "Female")
table(survey$gender)

# religion
survey$religion <- NA

#factuals
survey$factual1 <- NA
survey$factual2 <- NA
survey$factual3 <- NA

## alienation index
survey$dontcare <- dplyr::recode(survey$B1_1,
                                 `1` = "Feel",
                                 `2` = "Don t feel",
                                 `3` = "Not sure")
survey$dontcount <- dplyr::recode(survey$B1_3,
                                  `1` = "Feel",
                                  `2` = "Don t feel",
                                  `3` = "Not sure")
survey$leftout <- dplyr::recode(survey$B1_4,
                                `1` = "Feel",
                                `2` = "Don t feel",
                                `3` = "Not sure")

## question place
survey$question_place <- "before party"


# subset
survey_931108 <- survey[,c("pid", "study", "year", "urban", "region", "hh",
                           "inequality", "inequality.variable", "union.self", "union.other",
                           "employed", "employed.self", "occupation", "occupation.self", "hhsize", "educ", "income", 
                           "age", "race", "party", "ideology", "gender", "religion",
                           "factual1", "factual2", "factual3", "dontcare", "dontcount", "leftout",
                           "question_place")]

# save file
#saveRDS(survey_931108, file = "Harris_Data/survey_931108.rds")
